﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace ServiceTemplate
{
    public class XMLParser
    {
        private XmlTextReader textReader = null;
        private string xmlfile = null;
        private bool firstElement;


        XMLParser(string xmlfile)
        {
            this.xmlfile = xmlfile;
            firstElement = true;
        }

        public string readElementValue(string Name)
        {
            string value = null;
            textReader = new XmlTextReader(xmlfile);
            while (textReader.Read())
            {
                XmlNodeType nType = textReader.NodeType;
                if (nType == XmlNodeType.Element)
                {
                    if (textReader.Name.ToString().Equals(Name))
                    {
                        value = textReader.ReadElementContentAsString();
                        return value;
                    }
                }
            }
            if (value.Equals(null))
            { textReader = new XmlTextReader(xmlfile); }
            return value;
        }

        public string readElementNextValue(string Name)
        {
            string value = null;
            while (textReader.Read())
            {
                XmlNodeType nType = textReader.NodeType;
                if (nType == XmlNodeType.Element)
                {
                    if (textReader.Name.ToString().Equals(Name))
                    {
                        value = textReader.ReadElementContentAsString();
                        return value;
                    }
                }
            }
            if (value.Equals(null))
            { textReader = new XmlTextReader(xmlfile); }
            return value;
        }

        public void Close()
        {
            textReader.Close();
        }
    }
}
